/**
 * @author ashok
 * Chatter Notifier App.
 *
 * Utility class Containing Utility methods for chatter Notifier. 
 *
 * Copyright:  Copyright 2010
 * 
 * This file is part of Chatter Notifier.
 * 
 * Chatter Notifier is free software: you can redistribute it and/or modify
 * it under the terms of the New BSD license.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
public class ChatterUtil {
    
    private static final String January = 'January';
    private static final String February = 'February';
    private static final String March = 'March';
    private static final String April = 'April';
    private static final String May = 'May';
    private static final String June = 'June';
    private static final String July = 'July';
    private static final String August = 'August';
    private static final String September = 'September';
    private static final String October = 'October';
    private static final String November = 'November';
    private static final String December = 'December';
    
    private static final String SUNDAY = 'Sunday';
    private static final String MONDAY = 'Monday';
    private static final String TUESDAY = 'Tuesday';
    private static final String WEDNESDAY = 'Wednesday';
    private static final String THURSDAY = 'Thursday';
    private static final String FRIDAY = 'Friday';
    private static final String SATURDAY = 'Saturday';
    
    public static final String DIPLAY_ON_PROFILE = 'onProfile';
    public static final String DIPLAY_ON_FEED = 'onFeed';
    
    public static final String SHOW_FULL_DATE = 'Show my full birthday in my profile';
    public static final String SHOW_MONTH_DAY_ONLY = 'Show only month and day in my profile';
    public static final String SHOW_NONE = 'Don\'t show my birthday in my profile';
    
    // Use a reference date of March 8, 2009 which is a Sunday
    private static final Date REFERENCE_DATE = Date.newInstance(2009, 3, 8);
            
    public static String getMonthAsString(Date d) {     
        String monthStr = '';
        Integer month = d.month();
        
        if(month == 1) {
            monthStr = January;
        } else if (month == 2) {
            monthStr = February;
        } else if (month == 3) {
            monthStr = March;
        } else if (month == 4) {
            monthStr = April;
        } else if (month == 5) {
            monthStr = May;
        } else if (month == 6) {
            monthStr = June;
        } else if (month == 7) {
            monthStr = July;
        } else if (month == 8) {
            monthStr = August;
        } else if (month == 9) {
            monthStr = September;
        } else if (month == 10) {
            monthStr = October;
        } else if (month == 11) {
            monthStr = November;
        } else if (month == 12) {
            monthStr = December;
        }
        return monthStr;
    }
    
    public static String getDayOfWeek(Date dt) 
    {
        // get the number of days between the given date and the reference date (Sunday)
        Integer daysBetween = REFERENCE_DATE.daysBetween(dt);
        // get the remainder from dividing by the # of days in a week
        Integer remainder = Math.mod(daysBetween, 7);
        
        // NOTE: the if block on takes 2 script lines
        // a negative remainder indicates a past date from the reference
        if (remainder == 0)
           return SUNDAY;
        else if (remainder == 1 || remainder == -6)
           return MONDAY;
        else if (remainder == 2 || remainder == -5)
           return TUESDAY;
        else if (remainder == 3 || remainder == -4)
           return WEDNESDAY;
        else if (remainder == 4 || remainder == -3)
           return THURSDAY;
        else if (remainder == 5 || remainder == -2)
           return FRIDAY;
        else // remainder = 6 || remainder == -1
           return SATURDAY;
    }
    
    public static String getFormatedBirthdayDate(Date dt, String birthdayDiplayFormat, String displayAt) { 
        String formattedDate = '';
        if(dt != null) { 
            if(DIPLAY_ON_FEED.equalsIgnoreCase(displayAt)) {
            	// This done to get week day as per current year. Other wise weekday will be as per original date.
            	Date newDate = Date.newInstance(System.today().year(), dt.month(), dt.day());
                formattedDate = getDayOfWeek(newDate)+', ' + getMonthAsString(newDate) +' '+ newDate.day();
            } else if(DIPLAY_ON_PROFILE.equalsIgnoreCase(displayAt)) {
                formattedDate = getMonthAsString(dt)+' '+ dt.day()+', '+ dt.year();
                if(SHOW_FULL_DATE == birthdayDiplayFormat) {
                    formattedDate = getMonthAsString(dt)+' '+ dt.day()+', '+ dt.year();
                } else if(SHOW_MONTH_DAY_ONLY == birthdayDiplayFormat || SHOW_NONE == birthdayDiplayFormat) {
                    formattedDate = getMonthAsString(dt)+' '+ dt.day();
                } 
            }
        }
        return formattedDate;
    }
    
    /* Given method returns map of users (key as parentid and value as EntitySubscription) whom
     * i am(userId) following i.e is whom I am the subscrib to.
     */
    public static Map<String, EntitySubscription> getFollowingUserList(String userId) {
        List<EntitySubscription> followingES = [select id, parentid, subscriberid, parent.name from EntitySubscription 
                                                where subscriberid = :userId order by parentid];

        Map<String, EntitySubscription> following = new Map<String, EntitySubscription> ();
        // filter out all the followings users.
        for( EntitySubscription es : followingES ) {
            if( ('' + es.parentid).substring(0,3) == '005' ) {
                following.put(es.parentid, es); 
            }
        }
        return following;
    }
    
    public static List<EntitySubscription> getFollowers(String userId)  {
         // All users who are following us
        List<EntitySubscription> followers = [select id, subscriberid, subscriber.name
                                        from EntitySubscription  where parentid = :userId    //set the id of the user who you want to see all their followers
                                        order by subscriberid];       
        return followers;
     }
    
}